<template>
	<view class="flex flex-col h-screen">
		<NavBarVue title="我来解读"></NavBarVue>
		<view class="flex-1 overflow-hidden">
			<scroll-view scroll-y="true" class="h-full">
				<view class="p-30">
					<view class="bg-white rounded-md p-20 mt-20">
						<view class="mb-20">
							<view class="font-bold text-[#333333] text-sm mb-10">解读:</view>
							<uni-easyinput type="textarea" autoHeight v-model="formData.details"
								placeholder="请输入解读内容"></uni-easyinput>
						</view>
					</view>
					<view class="bg-white rounded-md p-20 mt-20">
						<view class="mb-20">
							<view class="font-bold text-[#333333] text-sm mb-10">上传图片:</view>
							<UpImageVue :src="formData.headimg" :limit="1" :image-styles="{width: 120, height: 120}" imgKey="headimg"
								@get-url="setUrl">
							</UpImageVue>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="px-30 bg-[#329EFF]">
			<view class="bg-[#329EFF] text-center text-white p-20 text-lg rounded-lg" @click="handleSubmit">提交</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted,
		getCurrentInstance
	} from 'vue';
	var vue3This = getCurrentInstance().proxy; //必须定义到最外面，放import后面即可 */
	import {
		onShow,
		onLoad,
	} from "@dcloudio/uni-app"

	import testApi from '@/apis/apiList/testApi'
	import userApi from '@/apis/apiList/userApi';
	import communityApi from '@/apis/apiList/communityApi';
	import UpImageVue from "@/components/UpImage.vue";
	import recordCompVue from './components/recordComp.vue';

	const id = ref('')
	const isMounted = ref(false)
	const loading = ref(false)
	const userId = ref('')
	const user = ref({})
	const anchor = ref({})
	const formData = ref({
		details: "",
		headimg: "",
		sts: 0
	})

	onMounted(() => {
		console.log(vue3This)
		//App的renderjs必须调用的函数，传入当前模块this
		// RecordApp.UniRenderjsRegister(vue3This);

		// isMounted.value=true; uniPage__onShow(); //onShow可能比mounted先执行，页面准备好了时再执行一次
	})

	onLoad((option) => {
		id.value = option.id;
		userId.value = uni.getStorageSync('user').id
		queryMemberInfo()
	})

	onShow(() => {
		// if(isMounted.value) uniPage__onShow(); //onShow可能比mounted先执行，页面可能还未准备好
	})

	const handleSubmit = () => {
		if (formData.value.details.length == 0) {
			uni.showToast({
				icon: 'none',
				title: '请文本或语音解读'
			})
			return
		}
		if (loading.value == true) {
			return
		}
		loading.value = true
		let opt = Object.assign({}, formData.value, {})
		opt['poss.id'] = id.value
		if (user.value.ach == 1) {
			opt['anchor.id'] = anchor.value.id
			opt.tou = 0
		}else {
			opt['member.id'] = userId.value
			opt.tou = 0
		}
		console.log(opt)
		communityApi.anchorpretPoss(opt).then(res => {
			uni.showToast({
				icon: 'none',
				title: '解读成功'
			})
			uni.navigateBack({
				delta: 1
			})
			loading.value = false
		})
	}

	// 获取个人信息
	const queryMemberInfo = () => {
		userApi.queryMemberInfo({
			id: userId.value
		}).then(res => {
			user.value = res.data
			console.log('用户信息', user.value);
			if (res.data.ach == 1) {
				queryAnchorInfoByMemberId()
			}
		})
	}
	// 获取主播信息
	const queryAnchorInfoByMemberId = () => {
		userApi.queryAnchorInfoByMemberId({
			"member.id": userId.value
		}).then(res => {
			anchor.value = res.data
		})
	}

	// 选择图片
	const setUrl = (key, url) => {
		console.log(key, url);
		formData.value.headimg = url
	}
</script>

<style lang="scss" scoped>
	.box-card {
		height: 490rpx;
		.card-title {
			background-color: #D9F1FF;
		}
		.card-num {
			font-weight: 600;
		}
		.border-dashed {
			border-color: #8DC2D3;
		}
	}
	.val {
		text-align: right;
	}
</style>